﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.DirectoryServices.AccountManagement;
using System.Web.Configuration;
using System.ServiceModel;
using System.Data.Linq.SqlClient;

namespace TestVoyage
{
    public class Common
    {
        CoastripDataContext DC = new CoastripDataContext();

        public Dictionary<short, string> GetWharfromDicByVnx(int Vnx)
        {
            Dictionary<short, string> DicResult = new Dictionary<short, string>();
            //该航次所有起点终点的组合
            var ReSect = from y in DC.Wharfiny
                         join f in DC.Wharfrom on y.Fnx equals f.Fnx
                         where (f.Vnx == Vnx)
                         select new
                         {
                             FromW = f.Wmx
                         };
            foreach (var c in ReSect.Distinct())
            {
                DicResult.Add(c.FromW, DC.Wharf.ToDictionary(y => y.Wmx)[c.FromW].Nym);
            }
            return DicResult;
        }

        private int MakeFlagNum(Dictionary<int, short> dic, short from, short dest)
        {
            //产生Flag字符串
            int iResult = 0;
            Char[] arrayFlag = new Char[dic.Count() - 1];
            char cFlag = '0';
            short sWharf = 0;
            for (int i = 0; i < dic.Count() - 1; i++)
            {
                dic.TryGetValue(i, out sWharf);
                if (sWharf == from) cFlag = '0';
                if (sWharf == dest) cFlag = '1';
                arrayFlag[i] = cFlag;
            }
            iResult = Convert.ToInt32(new String(arrayFlag), 2);
            return iResult;
        }

        public Dictionary<int, short> GetVoyageWharfDicByVnx(int Vnx)
        {
            var Res1 = from y in DC.Wharfiny
                       join f in DC.Wharfrom on y.Fnx equals f.Fnx
                       where (f.Vnx == Vnx)
                       select new
                       {
                           FromW = f.Wmx
                       };
            //按数量排序，靠后的码头在前，靠前的码头在后
            var Res2 = from r1 in Res1
                       group r1 by new
                       {
                           r1.FromW
                       }
                           into zz
                           orderby zz.Count()
                           select new
                           {
                               Key = zz.Key,
                               CountW = zz.Count()
                           };
            //按数量倒序排列，第一行即是终点码头
            var Res3 = from y in DC.Wharfiny
                       join f in DC.Wharfrom on y.Fnx equals f.Fnx
                       where (f.Vnx == Vnx)
                       select new
                       {
                           DestW = y.Wmx
                       };
            var Res4 = from r3 in Res3
                       group r3 by new
                       {
                           r3.DestW
                       }
                           into zzz
                           orderby zzz.Count() descending
                           select new
                           {
                               Key = zzz.Key,
                               CountW = zzz.Count()
                           };

            Dictionary<int, short> dWharf = new Dictionary<int, short>();
            //先添加终点
            dWharf.Add(0, Res4.First().Key.DestW);
            foreach (var c in Res2)
            {
                dWharf.Add(c.CountW, c.Key.FromW);
            }
            return dWharf;
        }

        public static string AClerkDisplay(string Acnt)
        {
            string sResult = string.Empty;

            if (Acnt == "" || Acnt == null)
                sResult = "";
            else
            {
                try
                {
                    sResult = Groupersons("AgentClerks")[Acnt];
                }
                catch (Exception)
                {
                    sResult = "";
                }
            }
            return sResult;
        }

        public static Dictionary<string, string> Groupersons(string gp)
        {
            Dictionary<string, string> usrs = new Dictionary<string, string>();
            PrincipalContext ptx = null;
            GroupPrincipal grp = null;
            try
            {
                ptx = new PrincipalContext(ContextType.Domain);
                grp = GroupPrincipal.FindByIdentity(ptx, gp);
            }
            catch (SystemException)
            {
                ptx = new PrincipalContext(ContextType.Machine);
                grp = GroupPrincipal.FindByIdentity(ptx, gp);
            }
            if (grp != null)
            {
                foreach (Principal p in grp.Members)
                {
                    usrs.Add(p.SamAccountName, p.DisplayName);
                }
                grp.Dispose();
            }
            if (ptx != null) ptx.Dispose();
            return usrs;
        }
        public static bool IsCredit(string gRole)
        {
            WindowsPrincipal wp = new WindowsPrincipal(ServiceSecurityContext.Current.WindowsIdentity);
            return wp.IsInRole(WebConfigurationManager.AppSettings[gRole]);
        }
        public static string Visitor()
        {
            WindowsPrincipal wp = new WindowsPrincipal(ServiceSecurityContext.Current.WindowsIdentity);
            return wp.Identity.Name.Split(new char[] { '\\' })[1];
        }

        //航运全价 = （舱位基本价 + 舱位软票费 + 码头空调费 + 港务费）-〉四舍五入取整 
        //航运折扣价 = （舱位基本价 x (1 - 舱位折扣率) + 舱位软票费 + 码头空调费 + 港务费）-〉四舍五入取整 
        private float doShip(byte DiscntType, float Discnt, float GFare, float BunkFee, float WharfFee, float WharfAir)
        {
            return DiscntType > 0 ? (float)Math.Floor((double)(GFare * (1 - Discnt) + BunkFee + WharfFee + WharfAir) + 0.5) :
                                (float)Math.Floor((double)(GFare + BunkFee + WharfFee + WharfAir) + 0.5);
        }

        //等级票价计算公式
        //舱位全价（包含本地车）= 航运全价 + 车站空调费 + 站务费 + 本地车费 
        //舱位全价（包含异地车）= 航运全价 + 异地车费 
        //舱位折扣价（包含本地车）= 航运折扣价 + 车站空调费 + 站务费 + 本地车费 
        //舱位折扣价（包含异地车）= 航运折扣价 + 异地车费 
        //舱位全价（包含本地车和异地车）= 航运全价 + 车站空调费 + 站务费 + 本地车费 + 异地车费 
        //舱位折扣价（包含本地车和异地车）= 航运折扣价 + 车站空调费 + 站务费 + 本地车费 + 异地车费
        private float doSellout(byte DiscntType, float Discnt, float GFare, float BunkFee, float WharfFee, float WharfAir,
                        float OFare, float TFare, float StopFee, float StopAir)
        {
            return doShip(DiscntType, Discnt, GFare, BunkFee, WharfFee, WharfAir) +
                (OFare > 0 ? OFare + StopFee + StopAir : 0) + TFare;
        }
        //载车票价 = （载车基本价 + 车型附加费 + 载车软票费）-〉四舍五入取整
        private float doRollout(float Fare, float Plus, float RollFee)
        {
            return (float)Math.Floor((double)(Fare + Plus + RollFee));
        }

        //行李票价 = （行李单价 x 行李重量 + 舱位软票费）-〉四舍五入取整
        private float doLuggout(float Fare, float Heft, float LuggFee)
        {
            return (float)Math.Floor((double)(Fare * Heft + LuggFee));
        }
        //舱位退款 = 舱位售出价 - （舱位售出价进10 x 舱位退票费率）
        private float doSellref(byte DiscntType, float Discnt, float GFare, float BunkFee, float WharfFee, float WharfAir,
                float OFare, float TFare, float StopFee, float StopAir, bool Refull, float Rate)
        {
            float fSellout = doSellout(DiscntType, Discnt, GFare, BunkFee, WharfFee, WharfAir,
                                        OFare, TFare, StopFee, StopAir);
            return fSellout - To10(fSellout) * (Refull ? 0 : Rate);
        }
        //载车退款 = 载车售出价 - （载车售出价进10 x 载车退票费率）
        private float doRollref(float Fare, float Plus, float RollFee, bool Refull, float Rate)
        {
            float fRollout = doRollout(Fare, Plus, RollFee);
            return fRollout - To10(fRollout) * (Refull ? 0 : Rate);
        }
        //行李退款 = 行李售出价 - （行李售出价进10 x 行李退票费率）
        private float doLuggref(float Fare, float Heft, float LuggFee, bool Refull, float Rate)
        {
            float fLuggout = doLuggout(Fare, Heft, LuggFee);
            return fLuggout - To10(fLuggout) * (Refull ? 0 : Rate);
        }


        //获取默认日期
        private DateTime DefaultDate()
        {
            return DateTime.Parse("1900-1-1");
        }

        //日期比较
        private int DateDiff(DateTime dtA, DateTime dtB)
        {
            return SqlMethods.DateDiffDay(dtA, dtB);
        }

        private float To10(float fNum)
        {
            float fResult = 0;
            string[] sNum = fNum.ToString().Split('.');
            string sTen = "";
            //if语句的else前面部分是判断小数的，注释后则不判断小数
            //if (sNum.Length == 2)
            //{
            //    if (int.Parse(sNum[1]) > 0 || int.Parse(sNum[0].Substring(sNum[0].Length - 1, 1)) > 0)
            //    {
            //        if (sNum[0].Length > 1)
            //        {
            //            sTen = sNum[0].Substring(0, sNum[0].Length - 1);
            //            if (sTen == "9")
            //                fResult = float.Parse((int.Parse(sTen) + 1).ToString().PadRight(sNum[0].Length + 1, '0'));
            //            else
            //                fResult = float.Parse((int.Parse(sTen) + 1).ToString().PadRight(sNum[0].Length, '0'));
            //        }
            //        else
            //            fResult = 10f;
            //    }
            //    else
            //        fResult = float.Parse(sNum[0]);
            //}
            //else
            //{
            if (int.Parse(sNum[0].Substring(sNum[0].Length - 1, 1)) > 0)
            {
                if (sNum[0].Length > 1)
                {
                    sTen = sNum[0].Substring(0, sNum[0].Length - 1);
                    if (sTen == "9")
                        fResult = float.Parse((int.Parse(sTen) + 1).ToString().PadRight(sNum[0].Length + 1, '0'));
                    else
                        fResult = float.Parse((int.Parse(sTen) + 1).ToString().PadRight(sNum[0].Length, '0'));
                }
                else
                    fResult = 10f;
            }
            else
                fResult = float.Parse(sNum[0]);
            //}
            return fResult;
        }

       
    }

  
}
